/*******************************************************************************
;******************************************************************************
*/

#include  <stdio.h>
#include  <math.h>
#include "hal.h"        /* Include HAL definition */    
#include "ClkGen.h"     /* Include Oggetto: Clock generator */
#include "InParser.h"   /* Include Oggetto: Keyboard Input Event generator */
#include "OutDrv.h"     /* Include Oggetto: OutDrv */

struct tFLASHRAM FR;

// Function prototypes

unsigned char dd[30];

/*
unsigned int cazzata(void){ 
      unsigned int res;
      unsigned char i;
      
      res=0;
      while(SIN.Start);
      while(!SIN.Start);
      for(i=0;i<=15;i++){ 
       while(SIN.Clk && (SIN.Start));
       res <<= 1;       
       if (SIN.Data) res|=1;
       while(!SIN.Clk && (SIN.Start));       
      }        
      return(res); 
} 
*/
const double Dfmax=2000.0;
       
void main(void)
{ 
  double x,f,f2;
  unsigned int i,p;
  unsigned long capt,C1_W=0xFFFFFFl,C2_W=0XFFFFFFl;
  
  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
      
  HAL_init();
  ClkGen_Init();
  InParser_Init();
  
  /*
  FR.P1=10;
  FR.P2=20;
  FR.P3=30;
  FR.P4=40;
  */
  
  i=p=0;
  // HAL_NV_Snapshot(FR);
  
  // HAL_NV_Write(NVRAM,FR);
  
  // HAL_NV_Write(NVRAM.P2,p);
  
  HAL_NV_Read(FR,NVRAM);
  
  while(1)          // Repeat forever
  {  
    
    OutDrv_Clk();    
    if(ClkGen.n.Clk16m)InParser_Clk();
    
    //if(ClkGen.n.Clk256m)
    // TACCTL1|=CCIS0;
    LED.DL1=1;
    //Disp_unsigned(cazzata()/13);
    //Disp_unsigned(i);
    if(ClkGen.n.Clk256m){
    //sprintf(dd,"=%3d",p);
    Disp_string(dd);
    }     
    LED.DL1= 0;
    LED.DL3= 0;
    LED.DL2= 0;
    
    if(ClkGen.n.Clk16m){
          capt=HAL_GetPeriodC1();
          if (capt>=0xFFFFFF) C1_W=0xFFFFFFFFl; else C1_W= (C1_W*63l+capt)/64l; 
          capt=HAL_GetPeriodC2();
          if (capt>=0xFFFFFF) C2_W=0xFFFFFFFFl; else C2_W= (C2_W*63l+capt)/64l;                     
    }


    if(ClkGen.n.Clk256m){
          capt=C1_W;
          if (capt > 0 ) f= 240000000.0 / capt;
          if (capt > (0xFFFFFFFEl)) f=0.0;
          
          capt=C2_W;    
          if (capt > 0 ) f2= 240000000.0 / capt;
          if (capt > (0XFFFFFFFEl)) f2=0.0;
          f=fabs(f-f2);
          if(f>Dfmax) sprintf(dd,"-AL-"); else sprintf(dd,"%4f",f);
    }   

    if(InParser.Value.name.P1 ) {
       
       if ((++i)==10000) i=0;
       LED.DL2= 1;
    //   
       
    //   sprintf(dd,"%4d",HAL_GetPeriodC1());
    
    //if(ClkGen.n.Clk1s)sprintf(dd,"-C1-"); 
    if(ClkGen.n.Clk256m){
          capt=C1_W;    
          if (capt > 0 ) f= 240000000.0 / capt;
          if (capt > (0XFFFFFFFEl)) sprintf(dd,"----"); else sprintf(dd,"%4f",f);
    }
    
       
       p=FR.P1;
      
    }         
    
    
    if(InParser.Value.name.P2 && InParser.Event.name.P2 ) {
     
       if( i == 0 ) i=10000;       
       i--;
       LED.DL3= 1;
       sprintf(dd,"%4d",i);
       
       //TACCTL1 &= ~CCIS0;
       //sprintf(dd,"-C2-");
       /*
       if(ClkGen.n.Clk256m){
          capt=C2_W;    
          //x= capt/4000.0;
          if (capt > 0 ) f= 240000000.0 / capt;
          if (capt > (0XFFFFFFFEl)) sprintf(dd,"----"); else sprintf(dd,"%4f",f);
       } */  
       
       p=NVRAM.P3;
    }
    
    
    ClkGen_Ack();
    InParser_Ack();
    
    //if(ClkGen.n.Clk128m)TACCTL1^=CCIS0;
    //if(ClkGen.n.Clk128m) sprintf(dd,"%4d",i);
    
  }
}